package cn.ekuma.android.ui.scan;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;

import com.caucho.hessian.client.HessianRuntimeException;
import com.openbravo.bean.Product;
import com.openbravo.data.basic.BasicException;

import cn.ekuma.android.ui.scan.history.HistoryActivity;
import cn.ekuma.android.ui.scan.history.HistoryManager;
import cn.ekuma.client.android.scantool.R;
import cn.ekuma.epos.android.Config;
import cn.ekuma.epos.android.finder.ProductDialyActivate;
import cn.ekuma.epos.datalogic.DataLogicPDA;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import unicall.unicom.uniwell.UpdateManager;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class CaptureScanToolActivity extends CaptureScanActivity {

    public static final int UPDATA_ID=ADVCODEINPUT_ID+1;
    private String customProductSearch;

    @Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
	    customProductSearch = prefs.getString(Config.KEY_CUSTOM_PRODUCT_SEARCH,
				   Config.KEY_CUSTOM_PRODUCT_SEARCH_DEFAULT);
		    if (customProductSearch != null && customProductSearch.trim().length() == 0) {
		    	 customProductSearch=Config.KEY_CUSTOM_PRODUCT_SEARCH_DEFAULT;
		    }
		   
		    try {
				DataLogicPDA.init(customProductSearch);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} 
	}

	protected void clearHistory(HistoryManager historyManager) {
		 
	}

    @Override
    boolean onFinderViewLongClicked() {
        openOptionsMenu();
        return true;
    }


    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, RESERT_ID, Menu.NONE, "返回")
                .setIcon(android.R.drawable.ic_menu_preferences);
        menu.add(Menu.NONE, HISTORY_ID, Menu.NONE, cn.ekuma.epos.ui.android.R.string.menu_history)
                .setIcon(android.R.drawable.ic_menu_recent_history);
        menu.add(Menu.NONE, SETTINGS_ID, Menu.NONE, cn.ekuma.epos.ui.android.R.string.menu_settings)
                .setIcon(android.R.drawable.ic_menu_preferences);
        menu.add(Menu.NONE, UPDATE_LOCAL_ID, Menu.NONE, cn.ekuma.epos.ui.android.R.string.menu_update_local)
                .setIcon(android.R.drawable.ic_menu_help);
        menu.add(Menu.NONE, ADVCODEINPUT_ID, Menu.NONE, cn.ekuma.epos.ui.android.R.string.adv_input_code)
                .setIcon(android.R.drawable.ic_menu_info_details);
        menu.add(Menu.NONE, UPDATA_ID, Menu.NONE, R.string.checkupdata)
                .setIcon(android.R.drawable.ic_menu_upload);
        return true;
    }

    // Don't display the share menu item if the result overlay is showing.

    public boolean onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        return true;
    }



    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
        switch (item.getItemId()) {
            case RESERT_ID:
                HashMap<String,Double> returnBean=historyManager.buildLocalHistory();
                if(!returnBean.isEmpty()){
                    Intent result = new Intent();
                    result.putExtra(SELECTED_BEANS, returnBean);
                    setResult(RESULT_OK, result);
                }
                finish();
                break;
            case HISTORY_ID:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                intent.setClassName(this, HistoryActivity.class.getName());
                startActivity(intent);
                break;
            case SETTINGS_ID:
                intent.setClassName(this, PreferencesActivity.class.getName());
                startActivity(intent);
                break;
            case UPDATE_LOCAL_ID:
                initLocalHost();
                break;
            case ADVCODEINPUT_ID:
                intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                intent.setClassName(this, ProductDialyActivate.class.getName());
                startActivity(intent);
                break;
            case UPDATA_ID:
                UpdateManager updateManager=new UpdateManager(this);
                updateManager.checkUpdateInfo(customProductSearch+"/scanTool_version.xml");
                break;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }


    private void initLocalHost() {
        final ProgressDialog dialog = ProgressDialog.show(this, "",
                "下载数据，请稍等 …", true, true);
        Thread t = new Thread(new Runnable() {

            public void run() {
                int countRecord=0;
                try {
                    productDAO.clear();
                    I_DataLogicSales basic =DataLogicPDA.getDLSales();
                    List<Product> listp=basic.list(Product.class);
                    if(!listp.isEmpty())
                        productDAO.insert(listp);
                    countRecord=listp.size();
                } catch (HessianRuntimeException e){
                }catch (BasicException e) {
                }
                dialog.dismiss();
                Looper.prepare();
                Toast.makeText(CaptureScanToolActivity.this, "总计更新：" + countRecord, Toast.LENGTH_SHORT).show();
                Looper.loop();
            }
        });
        t.start();
    }
}
